package io.gitee.seen.core.util;

import java.util.function.Consumer;

/**
 * 日志工具类
 *
 * @author chixuehui
 * @since 1.8
 */
public class LogUtils {

    private LogUtils() {
    }

    /**
     * 消息长度超过最大长度时，截取后添加···
     *
     * @param msg       实际消息
     * @param maxLength 最大长度
     * @return 截取后展示的消息
     */
    public static String maxLength(String msg, int maxLength) {

        if (msg == null) {
            return null;
        }
        int min = Math.min(msg.length(), maxLength - 3);
        return min == msg.length() ? msg : (msg.substring(0, min) + "···");
    }

    /**
     * 耗时长日志执行，耗时大于指定耗时时，打印warn日志
     *
     * @param expense      耗时毫秒
     * @param overExpense  超过的耗时
     * @param supplierWarn warn日志
     * @param supplierInfo info日志
     */
    public static void timeConsuming(long expense, long overExpense, Consumer<Long> supplierWarn,
                                     Consumer<Long> supplierInfo) {

        if (expense >= overExpense) {
            supplierWarn.accept(expense);
        } else {
            supplierInfo.accept(expense);
        }
    }
}
